package com.woniu.defecttracking.controller.datas;


import com.woniu.defecttracking.annotation.Mes;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.AddAccountDto;
import com.woniu.defecttracking.dto.PageAccountDto;
import com.woniu.defecttracking.entity.Account;
import com.woniu.defecttracking.entity.AccountDetails;
import com.woniu.defecttracking.exception.BugInfoException;
import com.woniu.defecttracking.param.*;
import com.woniu.defecttracking.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>

 * @author 何家欣
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    public void login(String username,String password){
    }

    /**
    * @作者: 倪西
    * @描述：未登录请求拦截
    * @时间：2022/1/7 21:49
    * @参数：[]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/notLogin")
    public Result notLogin(){
        return  Result.getInstance(null).setCode(405).setMessage("未登录");
    }

    @PostMapping("/findAccount")
    public Result findProject(@RequestBody FindAccountParam findAccountParam){
        PageAccountDto account = accountService.findAccount(findAccountParam);
        if (account==null){
            return Result.getInstance(account).setMessage("查询失败").setCode(500).setData(account);

        }
        return Result.getInstance(account).setMessage("查询成功").setCode(200).setData(account);
    }

    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable Integer id) {
        Account byId = accountService.getById(id);
        return Result.getInstance(byId);
    }

    @PostMapping("/addAccount")
    public Result addProject(@RequestBody AddAccountParam addProjectParam){
        AddAccountDto account = accountService.addAccount(addProjectParam);
        if (account==null){
            return Result.getInstance(account).setMessage("添加失败").setCode(500).setData(account);

        }
        return Result.getInstance(account).setMessage("添加成功").setCode(200).setData(account);
    }
    @PostMapping("/updateAccount")
    public Result updateProject(@RequestBody UpdateAccountParam updateProjectParam){
        boolean b = accountService.updateAccount(updateProjectParam);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
    }
    @Mes()
    @PostMapping("/updatePassword")
    public Result updateProject2(@RequestBody UpdateAccountParam updateProjectParam){
        boolean b = accountService.updateAccount(updateProjectParam);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
    }
    @PostMapping("/deleteAccount")
    public Result deleteProject(@RequestBody List<Integer> ids){
        boolean b = accountService.deleteAccount(ids);
        if (b==false){
            return Result.getInstance(false).setMessage("删除失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("删除成功").setCode(200);
    }

    @GetMapping("/toSuper/{id}")
    public Result toSuper(@PathVariable Integer id){
        boolean b = accountService.toSuper(id);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
    }

    @GetMapping("/isSuper")
    public Result isSuper(){
        boolean b = accountService.isSuper();
        if (b==false){
            return Result.getInstance(false).setMessage("这个没权限").setCode(500);

        }
        return Result.getInstance(true).setMessage("这个有权限").setCode(200);
    }

    @GetMapping("/mes")
    public Result getMessage() throws BugInfoException {
        AccountDetails accountDetails = (AccountDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String s = ComUtil.emailCode(6);
        stringRedisTemplate.opsForValue().set("woniu:message:"+accountDetails.getAccountId(),s,10, TimeUnit.MINUTES);
        if (accountDetails.getAccountEmail()==null){
            throw new BugInfoException("请先设置邮箱！",10086);
        }
        ComUtil.sendEmail("woniu验证码","您的验证码为："+s+"\n验证码将会在10分钟后失效",accountDetails.getAccountEmail());
        return Result.getInstance(null).setMessage("发送成功");
    }
}

